<?php
session_start();
require 'connection.php';
require 'util.php';
checkLoggedIn();
$addionalInfoExists = additionalInformationExists($_SESSION['userLoggedIn']);

if(isset($_POST["action"])){
	executeAction($_POST, $_POST["action"], $addionalInfoExists);
}else{
	returnHome();
}
function returnHome(){
	goToUrl(getURLFromReferer("home.php"));
}

function executeAction($p, $action, $addionalInfoExists){
	switch($action){
		case "requestProfile":
			sendProfile($addionalInfoExists);
			break;
		case "showProfile":
			showProfile();
			break;
		case "getProfileEditor":
			getEditProfile();
			break;
		case "getListingEditor":
			getListingEditor();
			break;
		case "updateListing":
			saveListing($_POST, $_FILES);
			break;
		default:
			returnHome();
			break;
	}
}

function getEditProfile(){
	$noPicUrl = $GLOBALS['noPicImgUrl'];
	$info = getUserInformation(getLoggedInUser());
	$firstName = $info['first_name'];
	$secondName = $info['second_name'];
	$firstLastname = $info['first_lastname'];
	$secondLastname = $info['second_lastname'];
	$postcode = $info['postcode'];
	$address = $info['address'];
	$gender = $info['gender'];
	$dateOfBirth = $info['date_of_birth'];
	//$dateOfBirth = formatDateString($dateOfBirth, 'm/d/Y', 'Y-m-d');
	$age = getAge($dateOfBirth);
	$firstImageUrl = ($info['first_image_url'] == "" ? $noPicUrl : $info['first_image_url']);
	$secondImageUrl = ($info['second_image_url'] == "" ? $noPicUrl : $info['second_image_url']);
	$htmlResponse = "
		<div id=\"editProfileFormContainer\">
			<div id=\"CPFCTheEditForm\">
				<form name=\"editProfileForm\" enctype=\"multipart/form-data\" method=\"post\" action=\"accountValidator.php\" >
				<div id=\"CPFCEditName\">
					<p>Name</p>
					<div>
						<label for=\"firstName\">First Name:</label>
						<input id=\"firstName\" name=\"firstName\" type=\"text\" value=\"{$firstName}\"></input>
						<label for=\"secondName\">Second Name:</label>
						<input id=\"secondName\" name=\"secondName\" type=\"text\" value=\"{$secondName}\"></input>
					</div>
					<div>
						<label for=\"firstLastname\">First Family Name:</label>
						<input id=\"firstLastname\" name=\"firstLastname\" type=\"text\" value=\"{$firstLastname}\"></input>
						<label for=\"secondLastname\">Second Family Name:</label>
						<input id=\"secondLastname\" name=\"secondLastname\" type=\"text\" value=\"{$secondLastname}\"></input>
					</div>
				</div>
				<div id=\"CPFCEditAddress\">
					<p>Address (we won't make it public): </p>
					<label for=\"postcode\">Postcode:</label>
					<input type=\"text\" name=\"postcode\" id=\"postcode\" value=\"{$postcode}\"></input>
					<label for=\"address\">Address: </label>
					<input type=\"text\" name=\"address\" id=\"address\" value=\"{$address}\"></input>
				</div>
				<div id=\"CPFCEditAditionalInfo\">
					<p>Additional information: </p>
					<div>
						<label for=\"gender\">Gender:</label>
						<input type=\"radio\" name=\"gender\" id=\"genderM\" value=\"1\" ".($gender == "Male" ? "checked":"").">Male</input>
						<input type=\"radio\" name=\"gender\" id=\"genderF\" value=\"2\" ".($gender == "Female" ? "checked":"").">Female</input>
					</div>
					<label for=\"dateOfBirth\">Date of birth:</label>
					<input type=\"date\" name=\"dateOfBirth\" id=\"dateOfBirth\" value=\"{$dateOfBirth}\">
				</div>
				<div id=\"CPFCEditPhotos\">
					<p>Please upload at least one image:</p>
					<div>
						<label for=\"primaryImage\">Main Image: </label>
						<div id=\"primaryImagePreviewer\" style=\"background-image: url('{$firstImageUrl}')\">
						</div>
						<a href=\"#\">Add Image</a>
						<input id=\"primaryImage\" name=\"primaryImage\" type=\"file\" accept=\"image/*\" />
					</div>
					<div>
						<label for=\"secondaryImage\">Secondary Image: </label>
						<div id=\"secondaryImagePreviewer\" style=\"background-image: url('{$secondImageUrl}')\">
						</div>
						<a href=\"#\">Add Image</a>
						<input id=\"secondaryImage\" name=\"secondaryImage\" type=\"file\" accept=\"image/*\" /> 
					</div>
				</div>
				<input type=\"hidden\" value=\"editProfile\" name=\"action\" />
				</form>
			</div>
			<div id=\"CPFCTheButtons\">
				<a id=\"CPFCCompleteEdit\" href=\"#\" class=\"nextBackButton\">Submit</a>
				<a id=\"CPFCExitEditProfile\" href=\"#\" class=\"nextBackButton\">Cancel</a>
			</div>
		</div>
		";

	$response = array(
		"error" => false,
		"htmlString"=>$htmlResponse
		);
	echo getJSON($response);
}

function sendProfile($addionalInfoExists){
	$htmlResponse = "";
	$response = true;
	if(!$addionalInfoExists){
		$htmlResponse = "
		<div id=\"completeProfileFormContainer\" class=\"loginLayerContentsBox\">
			<div id=\"CPFCTheForm\">
				<form name=\"completeProfileForm\" enctype=\"multipart/form-data\" method=\"post\" action=\"accountValidator.php\" >
				<p>We're almost done...</p>
				<div id=\"CPFCName\" class=\"CPFCformPart\">
					<p>Please tell us who you are:</p>
					<div>
						<label for=\"firstName\">First Name:</label>
						<input id=\"firstName\" name=\"firstName\" type=\"text\"/>
						<label for=\"secondName\">Second Name:</label>
						<input id=\"secondName\" name=\"secondName\" type=\"text\"><br>
					</div>
					<div>
						<label for=\"firstLastname\">First Family Name:</label>
						<input id=\"firstLastname\" name=\"firstLastname\" type=\"text\"/>
						<label for=\"secondLastname\">Second Family Name:</label>
						<input id=\"secondLastname\" name=\"secondLastname\" type=\"text\">
					</div>
				</div>
				<div id=\"CPFCAddress\" class=\"CPFCformPart\">
					<p>Please give us your address (we won't make it public): </p>
					<label for=\"postcode\">Postcode:</label>
					<input type=\"text\" name=\"postcode\" id=\"postcode\"/>
					<label for=\"address\">Address: </label>
					<input type=\"text\" name=\"address\" id=\"address\" />
				</div>
				<div id=\"CPFCAdditionalInfo\" class=\"CPFCformPart\">
					<p>Additional information: </p>
					<div>
						<label for=\"gender\">Gender:</label>
						<input type=\"radio\" name=\"gender\" id=\"genderM\" value=\"1\" checked>Male</input>
						<input type=\"radio\" name=\"gender\" id=\"genderF\" value=\"2\">Female</input>
					</div>
					<label for=\"dateOfBirth\">Date of birth:</label>
					<input type=\"date\" name=\"dateOfBirth\" id=\"dateOfBirth\"/>
				</div>
				<div id=\"CPFCPhotos\" class=\"CPFCformPart\">
					<p>Please upload at least one image:</p>
					<div>
						<label for=\"primaryImage\">Main Image: </label>
						<div id=\"primaryImagePreviewer\">
						</div>
						<a href=\"#\">Add Image</a>
						<input id=\"primaryImage\" name=\"primaryImage\" type=\"file\" accept=\"image/*\" />
					</div>
					<div>
						<label for=\"secondaryImage\">Secondary Image: </label>
						<div id=\"secondaryImagePreviewer\">
						</div>
						<a href=\"#\">Add Image</a>
						<input id=\"secondaryImage\" name=\"secondaryImage\" type=\"file\" accept=\"image/*\" /> 
					</div>
				</div>
				<input type=\"hidden\" value=\"completeRegistration\" name=\"action\" />
				</form>
			</div>
			<div id=\"CPFCTheButtons\">
				<a id=\"CPFCBack\" href=\"#\" class=\"nextBackButton\"> < Back</a>
				<a id=\"CPFCNext\" href=\"#\" class=\"nextBackButton\">Next > </a>
				<a id=\"CPFCComplete\" href=\"#\" class=\"nextBackButton\">Submit</a>
			</div>
		</div>
		";
		$response = false;
	}else{
		$response = true;

	}
	$arr = array(
		'response' => $response,
		'htmlString' => $htmlResponse 
		);
	echo getJSON($arr);
}

function showProfile(){
	$error = false;
	$htmlProfile = getProfileHtml();
	$htmlListing = getHtmlListing();
	$htmlString = "<div id=\"profileAndListingContainer\" class=\"loginLayerContentsBox\">".$htmlProfile.$htmlListing."</div>";
	$response = array(
		"error"=>$error,
		"htmlString"=>$htmlString
		);
	echo getJSON($response);
}

function getProfileHtml(){
	$noPicUrl = $GLOBALS['noPicImgUrl'];
	$info = getUserInformation(getLoggedInUser());
	$firstName = $info['first_name'];
	$secondName = $info['second_name'];
	$firstLastname = $info['first_lastname'];
	$secondLastname = $info['second_lastname'];
	$postcode = $info['postcode'];
	$address = $info['address'];
	$gender = $info['gender'];
	$dateOfBirth = $info['date_of_birth'];
	$dateOfBirth = formatDateString($dateOfBirth, 'Y-m-d', 'm/d/Y');
	$age = getAge($dateOfBirth);
	$firstImageUrl = $info['first_image_url'];
	$secondImageUrl = $info['second_image_url'];
	$htmlProfile =
	"<div id=\"profileContainer\">
		<div id=\"profileImageContainer\">
		<img src=\"".($firstImageUrl == "" ? ($secondImageUrl == "" ? $noPicUrl : $secondImageUrl) : $firstImageUrl)."\" />
		</div>
		<div id=\"profileInfoContainer\">
		<p>$firstName".($secondName == "" ? "": " $secondName") ." $firstLastname".($secondLastname == "" ? "": " $secondLastname")."</p>
		<a href=\"#\" id=\"editProfileButton\" class=\"formButtonClass grayButton2 \">Edit profile</a>
		</div>
	</div>
	";
	return $htmlProfile;
}

function getHtmlListing(){
	$listingExists = listingExists(getLoggedInUser());
	$htmlString = "";
	$error = false;
	if(!$listingExists)
	{
		$htmlString = "
		<div id=\"listingWrapper\">
			<h3>It seems you don't have a listing created yet.</h3>
			<a id=\"createListingButton\" href=\"#\">You can start creating your listing here.</a>
		</div>
		";
	}
	else
	{
		$listing = getListing(getLoggedInUser());
		$listing = $listing[0];
		//print_r($listing);
		$pk = $listing['pk'];
		$type = $listing['result_type_fk'];
		$title = $listing['title'];
		$image_url = $listing['image_url'] == "" ? $GLOBALS['noPicImgUrl'] : $listing['image_url'];
		$telephone = $listing['telephone'];
		$description = $listing['description'];
		$price = $listing['price'];
		$user = $listing['user_fk'];
		$email = $listing['email'];
		$webpage = $listing['webpage'];
		$roomType = $listing['room_type_fk'];
		$bedType = $listing['bed_type_fk'];
		$accomodates = $listing['accomodates'];
		$bedrooms = $listing['bedrooms'];
		$beds = $listing['beds'];
		$extraPeople = $listing['extra_people'];
		$weekPrice = $listing['week_price'];
		$monthPrice = $listing['month_price'];
		$cancellationFee = $listing['cancellation_fee'];
		$rules = $listing['rules'];
		$cancellationType = $listing['cancellation_type_fk'];
		//$address = ($listing['house'] == "" ? "" : $listing['house']).($listing['street'] == "" ? "" : $listing['street']).($listing['district'] == "" ? "" : $listing['district']).($listing['city'] == "" ? "" : $listing['city']);
		$postcode = $listing['postcode'];
		$city = $listing['city'];
		$street = $listing['street'];
		$house = $listing['house'];
		$district = $listing['district'];
		$rating = $listing['rating'];
		$lat = $listing['lat'];
		$lng = $listing['lng'];
		$addressPk = $listing['addressPk'];
		$locationPk = $listing['locationPk'];
		$htmlString = "<div id=\"fullListingDisplayer\">
			<div id=\"listingInfo\">
			<div id=\"LIImage\">
				<img src=\"{$image_url}\">
			</div>
			<div id=\"LIContents\">
				<div id=\"LITitle\">
					<h2>{$title}</h2>
					<h2>{$price}</h2>
				</div>
				<div id=\"LIDescription\">

					<div id=\"LIAddress\">
						<h3>{$city}, {$district}, {$street}</h3>
					</div>

					<div id=\"LIDescription\">
						".
							displayTextIfExists($description, "Description")
							.displayTextIfExists($weekPrice, "Week Price")
							.displayTextIfExists($monthPrice, "Month Price")
							.displayTextIfExists($rules, "Rules")
							.displayTextIfExists($accomodates, "Accomodates")
							.displayTextIfExists($bedrooms, "Bedrooms")
							.displayTextIfExists($beds, "Beds")
							.displayTextIfExists($cancellationType, "Cancellation")
							.displayTextIfExists($cancellationFee, "Cancellation Fee")
							.displayTextIfExists($webpage, "Webpage")
						."

					</div>
				</div>
				<a id=\"createListingButton\" class=\"nextBackButton\" href=\"#\">Edit</a>
			</div>
		</div>
		</div>";
	}
	return $htmlString;
}

//Listing logic

function listingExists($user){
	$result = getListing($user);
	return !empty($result);
}

function getListing($user){
	$query = "SELECT result.*, address.postcode AS 'postcode',
	address.pk AS 'addressPk',
	 address.street AS 'street',
	  address.district AS 'district',
	   address.city AS 'city',
	    address.house AS 'house',
	     location.latitude AS 'lat',
	     location.pk AS 'locationPk',
	      location.longitude AS 'lng' FROM result INNER JOIN address ON result.address_fk = address.pk INNER JOIN location ON address.location_fk = location.pk WHERE user_fk = :user_fk";
	$params = array(
		":user_fk"=>$user
		);
	$result = getResultArray($query, getPdo(), $params);
	return $result;
}

function saveListing($post, $files){
	$pdo = $GLOBALS['pdo'];
	$pk = $post['exant'];
	$isHotel = $post['typeofListing'] == "1";
	$isHouse = $post['typeofListing'] == "2";
	$isUpdate = listingExists(getLoggedInUser());
	try{
		$pdo->beginTransaction();
		$paramsCoords = array(
			":latitude"=>$post['lat'],
			":longitude"=>$post['lng']
			);
		if($isUpdate){
			$paramsWhere = array(
				":pk"=>$post['exantLocation']
				);
			$locationFk = executeUpdate("location", $paramsCoords, $paramsWhere, $pdo);
		}else{
			$locationFk = executeInsert("location", $paramsCoords, $pdo);
		}
		
		$paramsAddress = array(
			":postcode"=>$post['postcode'],
			":street"=>$post['street'],
			":district"=>$post['area'],
			":city"=>$post['city'],
			":house"=>$post['houseNumber'],
			":location_fk"=>$locationFk
			);
		if($isUpdate){
				$paramsWhere = array(
				":pk"=>$post['exantAddress']
				);
			unset($paramsAddress[":location_fk"]);
			$addressFk = executeUpdate("address", $paramsAddress, $paramsWhere, $pdo);
		}else{
			$addressFk = executeInsert("address", $paramsAddress, $pdo);
		}

		$params = array(
			":title"=> $post['name'],
			":image_url"=> "",
			":description"=> $post['description'],
			":price"=> $post['price'],
			":user_fk"=> getLoggedInUser(),
			":result_type_fk"=> $post['typeofListing'],
			":telephone"=> $post['telephone'],
			":email"=> $post['email'],
			":webpage"=> $post['webpage'],
			":room_type_fk"=> $post['roomType'],
			":bed_type_fk"=> $post['bedType'],
			":accomodates"=> $post['accomodates'],
			":bedrooms"=> $post['bedrooms'],
			":beds"=> $post['beds'],
			":extra_people"=> intval($post['extraPeople']),
			":week_price"=> $post['weekPrice'],
			":month_price"=> $post['monthPrice'],
			":cancellation_fee"=> floatval($post['cancellationFee']),
			":rules"=> $post['typeofListing'] == "1" ? $post['rulesHouse'] : ($post['typeofListing'] == "2" ? $post['rulesHotel'] : ""),
			":cancellation_type_fk"=> $post['cancellation'],
			":address_fk"=> $addressFk,
			":rating"=> $isHotel ? intval($post['hotelRating']) : 0
			);
		if($isUpdate){
			$paramsWhere = array(
			":pk"=>$post['exant']
			);
			unset($params[":address_fk"]);
			executeUpdate("result", $params, $paramsWhere, $pdo);
		}else{
			$listingPk = executeInsert("result", $params, $pdo);
		}
		$dir = createDirectory("img/profiles/".getLoggedInUser()."/listing");
		$filePaths = moveFiles($files, $dir);
		if($filePaths && !empty($filePaths)){
			$params = array();
			$params[':image_url'] = $filePaths['imageUrl'];
			$paramsWhere = array(
				":user_fk"=>getLoggedInUser()
				);
			executeUpdate("result", $params, $paramsWhere, $pdo);
		}
		$pdo->commit();
		$response = array(
			"error"=> false,
			"message" => "allOk"
			);
		echo getJSON($response);
	}catch(PDOException $e) {
		$pdo->rollback();
		echo $e;
		throw $e;
	}
}

function displayRoomTypes($type = ""){
	$rt = getListingRoomTypes();
	$types = "";
  	foreach ($rt as $key => $value) {
  		$pk = $value['pk'];
  		$description = $value['description'];
  		if($type != "" && $type == $pk){
  			$types .= "<option value='$pk' selected>$description</option>";
		}
		else{
			$types .= "<option value='$pk'>$description</option>";
		}
  		
  	}
  	return $types;
}

function displayBedTypes($type = ""){
	$rt = getListingBedTypes();	
	$roomTypes = "";
	foreach ($rt as $key => $value) {
		$pk = $value['pk'];
		$description = $value['description'];
		if($type != "" && $type == $pk){
			$roomTypes .= "<option value='$pk' selected>$description</option>";
		}
		else{
			$roomTypes .= "<option value='$pk'>$description</option>";
		}
  		
  	}

  	return $roomTypes;
}

function displayCancellationTypes($type = ""){
	$rt = getListingCancellationTypes();
	$cancellationTypes = "";
	foreach ($rt as $key => $value) {
	  	$pk = $value['pk'];
		$description = $value['description'];
		if($type != "" && $type == $pk){
			$cancellationTypes .= "<option value='$pk' selected>$description</option>";
		}
		else{
			$cancellationTypes .= "<option value='$pk'>$description</option>";
		}
  	}

  	return $cancellationTypes;
}

function displayListingCathegories($type = ""){
	$rt = getListingCathegories();
	$listingCathegories = "";
	foreach ($rt as $key => $value) {
		$pk = $value['pk'];
  		$description = $value['description'];
  		if($type != "" && $type == $pk){
  			$listingCathegories .= "<option value='$pk' selected>$description</option>";
  		}else{
  			$listingCathegories .= "<option value='$pk'>$description</option>";
  		}
  		
  	}	

  	return $listingCathegories;
}

function displayGenders($gender = ""){
	$rt = getGenders();
	$genders = "";
	foreach ($rt as $key => $value) {
		$pk = $value['pk'];
  		$description = $value['description'];
  		if($gender != "" && $gender == $pk){
  			$genders .= "<input type=\"radio\" name=\"gender\" id=\"genderM\" value=\"$pk\" checked>$description</input>";
		}
		else{
			$genders .=  "<input type=\"radio\" name=\"gender\" id=\"genderM\" value=\"$pk\">$description</input>";
		}
  		
  	}	

  	return $genders;
}

function getListingEditor(){
	$pk;
	$type;
	$title;
	$image_url;
	$telephone;
	$description;
	$price;
	$user;
	$email;
	$webpage;
	$roomType;
	$bedType;
	$accomodates;
	$bedrooms;
	$beds;
	$extraPeople;
	$weekPrice;
	$monthPrice;
	$cancellationFee;
	$rules;
	$cancellationType;
	//$address = ($listing['house'] == "" ? "" : $listing['house']).($listing['street'] == "" ? "" : $listing['street']).($listing['district'] == "" ? "" : $listing['district']).($listing['city'] == "" ? "" : $listing['city']);
	$postcode;
	$city;
	$street;
	$house;
	$district;
	$rating;
	$lat;
	$lng;
	$addressPk;
	$locationPk;
	if(listingExists(getLoggedInUser())){
				$listing = getListing(getLoggedInUser());
				$listing = $listing[0];
				//print_r($listing);
				$pk = $listing['pk'];
				$type = $listing['result_type_fk'];
				$title = $listing['title'];
				$image_url = $listing['image_url'] == "" ? $GLOBALS['noPicImgUrl'] : $listing['image_url'];
				$telephone = $listing['telephone'];
				$description = $listing['description'];
				$price = $listing['price'];
				$user = $listing['user_fk'];
				$email = $listing['email'];
				$webpage = $listing['webpage'];
				$roomType = $listing['room_type_fk'];
				$bedType = $listing['bed_type_fk'];
				$accomodates = $listing['accomodates'];
				$bedrooms = $listing['bedrooms'];
				$beds = $listing['beds'];
				$extraPeople = $listing['extra_people'];
				$weekPrice = $listing['week_price'];
				$monthPrice = $listing['month_price'];
				$cancellationFee = $listing['cancellation_fee'];
				$rules = $listing['rules'];
				$cancellationType = $listing['cancellation_type_fk'];
				//$address = ($listing['house'] == "" ? "" : $listing['house']).($listing['street'] == "" ? "" : $listing['street']).($listing['district'] == "" ? "" : $listing['district']).($listing['city'] == "" ? "" : $listing['city']);
				$postcode = $listing['postcode'];
				$city = $listing['city'];
				$street = $listing['street'];
				$house = $listing['house'];
				$district = $listing['district'];
				$rating = $listing['rating'];
				$lat = $listing['lat'];
				$lng = $listing['lng'];
				$addressPk = $listing['addressPk'];
				$locationPk = $listing['locationPk'];
			}

			$listingCathegories = displayListingCathegories(ifsetor($type, ""));
			$roomTypes = displayRoomTypes(ifsetor($roomType));
			$bedTypes = displayBedTypes(ifsetor($bedType));
			$cancellationTypes = displayCancellationTypes(ifsetor($cancellationType));
			$htmlString = "<div id=\"listingFormContainer\">
			<form method=\"post\" enctype=\"multipart/form-data\" id=\"listingForm\" action=\"profile.php\">
				<div id=\"typeOfListingContainer\">
					<label for=\"typeOfListing\">Please select a type of listing: </label>
					<select name=\"typeofListing\" id=\"typeofListing\">
						{$listingCathegories}
					</select>
				</div>
				
				<div id=\"listingFormBody\">
					<div>
						<!--NAME OF LISTING -->
						<label for=\"nameListing\">Name: </label>
						<input type=\"text\" name=\"name\" id=\"nameListing\" value=\"".ifsetor($title, "")."\"/>
						<label for=\"descriptionListing\">Description: </label>
						<textarea name=\"description\" id=\"descriptionListing\">".ifsetor($description, "")." </textarea> 
						<!--POSTCODE -->
						<label for=\"postcodeListing\">Postcode: </label>
						<input type=\"text\" name=\"postcode\" id=\"postcodeListing\" value=\"".ifsetor($postcode, "")."\"/>
						<!--FULL ADDRESS -->
						<label for=\"cityListing\">City: </label>
						<input type=\"text\" name=\"city\" id=\"cityListing\" value=\"".ifsetor($city, "")."\"/>
						<label for=\"areaListing\">District/Area: </label>
						<input type=\"text\" name=\"area\" id=\"areaListing\" value=\"".ifsetor($district, "")."\"/>
						<label for=\"streetNameListing\">Street Name: </label>
						<input type=\"text\" name=\"street\" id=\"streetNameListing\" value=\"".ifsetor($street, "")."\"/>
						<label for=\"houseNumberListing\">House Number (this remains private): </label>
						<input type=\"text\" name=\"houseNumber\" id=\"houseNumberListing\" value=\"".ifsetor($house,"")."\"/>
						<input type=\"hidden\" id=\"coordsLatListing\" name=\"lat\" value=\"".ifsetor($lat, "")."\">
						<input type=\"hidden\" id=\"coordsLngListing\" name=\"lng\" value=\"".ifsetor($lng, "")."\">
						
					</div>
					<div class=\"hideClass\">
						<!--TELEPHONE NUMBER -->
						<label for=\"telephoneListing\">Telephone: </label>
						<input type=\"number\" name=\"telephone\" id=\"telephoneListing\" value=\"".ifsetor($telephone, "")."\"/>
						<!--EMAIL -->
						<label for=\"emailListing\">Email: </label>
						<input type=\"email\" name=\"email\" id=\"emailListing\" value=\"".ifsetor($email,"")."\"/>
						<!--WEBPAGE ADDRESS -->
						<label for=\"webpageAddressListing\">Webpage Address: </label>
						<input type=\"text\" name=\"webpage\" placeholder=\"http://www.mysite.com\" id=\"webpageAddressListing\" value=\"".ifsetor($webpage, "")."\"/>
						<!--PRICE -->
						<label for=\"priceListing\">Price: </label>
						<input type=\"number\" name=\"price\" id=\"priceListing\" value=\"".ifsetor($price,"")."\"/>
						<!--WEEKLY PRICE -->
						<label for=\"weekPriceListing\">Weekly Price: </label>
						<input type=\"number\" name=\"weekPrice\" id=\"weekPriceListing\" value=\"".ifsetor($weekPrice, "")."\"/>
						<!--MONTHLY PRICE -->
						<label for=\"monthPriceListing\">Monthly Price: </label>
						<input type=\"number\" name=\"monthPrice\" id=\"monthPriceListing\" value=\"".ifsetor($monthPrice, "")."\"/>
					</div>

					<div class=\"hideClass\">
						<!--TYPE OF ROOM -->
						<label for=\"roomTypeListing\">Room Type: </label>
						<select name=\"roomType\" id=\"roomTypeListing\">
							{$roomTypes}
						</select>
						<!--TYPE OF BED -->
						<label for=\"bedTypeListing\">Bed Type: </label>
						<select name=\"bedType\" id=\"bedTypeListing\">
							{$bedTypes}
						</select>
						<!--NUMBER OF ACCOMODATES -->
						<label for=\"accomodatesListing\">Number of Accomodates</label>
						<input type=\"text\" name=\"accomodates\" id=\"accomodatesListing\" value=\"".ifsetor($accomodates,"")."\" />
						<!--BEDROOMS -->
						<label for=\"bedroomsListing\">Bedrooms: </label>
						<input type=\"text\" name=\"bedrooms\" id=\"bedroomsListing\" value=\"".ifsetor($bedrooms,"")."\"/>
						<!--BEDS -->
						<label for=\"bedsListing\">Beds: </label>
						<input type=\"text\" name=\"beds\" id=\"bedsListing\" value=\"".ifsetor($beds, "")."\" />
						<!--EXTRA PEOPLE -->
						<label for=\"extraPeopleListing\">Extra People: </label>
						<select name=\"extraPeople\" id=\"extraPeopleListing\">
						  <option value=\"\">select a value</option> 
						  <option value=\"1\"".printSelected(ifsetor($extraPeople), "1").">allowed</option>
						  <option value=\"0\" ".printSelected(ifsetor($extraPeople), "0").">not allowed</option>
						</select>
					</div>
					<div class=\"hideClass\">
						<!--CANCELLATION -->
						<label for=\"cancellationListing\">Cancellation</label>
						<select name=\"cancellation\" id=\"cancellationListing\">
							{$cancellationTypes}
						</select>
						<label for=\"cancellationFeeListing\">Cancellation Fee:</label>
						<input type=\"number\" name=\"cancellationFee\" id=\"cancellationFeeListing\" value=\"".ifsetor($cancellationFee,"")."\"/>
						<!--HOUSE RULES -->
						<label for=\"houseRulesListing\">House Rules</label>
						<textarea name=\"rulesHouse\" id=\"houseRulesListing\">".(ifsetor($type) == "1" ? ifsetor($rules) : "")."</textarea>
						<!--HOTEL RULES -->
						<label for=\"hotelRulesListing\">Hotel Rules</label>
						<textarea name=\"rulesHotel\" id=\"hotelRulesListing\">".(ifsetor($type) == "2" ? ifsetor($rules) : "")."</textarea>
						<!--HOTEL RATING -->
						<label for=\"hotelRatingListing\">Hotel Rating</label>
						<select name=\"hotelRating\" id=\"hotelRatingListing\">
						  <option value=\"\">select a value</option>
						  <option value=\"1\" ".printSelected(ifsetor($rating), "1").">1 star</option>
						  <option value=\"2\" ".printSelected(ifsetor($rating), "2").">2 star</option>
						  <option value=\"3\" ".printSelected(ifsetor($rating), "3").">3 star</option>
						  <option value=\"4\" ".printSelected(ifsetor($rating), "4").">4 star</option>
						  <option value=\"5\" ".printSelected(ifsetor($rating), "5").">5 star</option>
						</select>

					</div>
					<div id=\"listingPicturesContainer\" class=\"hideClass\">
						<label for=\"imageUrlListing\">Image: </label>
						<div id=\"imageUrlListingPreviewer\" ".(ifsetor($image_url) != "" ? "style=\"background-image: url('$image_url'); display:block;\"" : "")."></div>
						<a id=\"uploadListingImageButton\">Upload an image</a>
						<input id=\"imageUrlListing\" name=\"imageUrl\" type=\"file\" accept=\"image/*\" /> 
						
						<input type=\"hidden\" name=\"exantLocation\" value=\"".ifsetor($locationPk,"")."\">
						<input type=\"hidden\" name=\"exantAddress\" value=\"".ifsetor($addressPk,"")."\">
						<input type=\"hidden\" name=\"exant\" value =\"".ifsetor($pk,"")."\"> 
						<input type=\"hidden\" name=\"action\" value=\"updateListing\"/>
					</div>
				</div>
				<div id=\"listingEditorFormControlButtonsContainer\">
					<a id=\"LEFCBack\" href=\"#\" class=\"nextBackButton\"> < Back</a>
					<a id=\"LEFCNext\" href=\"#\" class=\"nextBackButton\">Next > </a>
					<a id=\"LEFCComplete\" href=\"#\" class=\"nextBackButton hideClass\">Submit</a>
					<a id=\"LEFCCancel\" href=\"#\" class=\"nextBackButton\">Cancel </a>
				</div>
			</form>
		</div>";
		//echo $htmlString;
		$response = array(
			"error"=> false,
			"htmlString"=>$htmlString
			);

		echo getJSON($response);
}

?>